﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//该类存储的是有关用户信息、Cookie、页面权限、按钮权限的有关方法
//以及系统常用的常量
namespace PMS
{
    public class OperateContext
    {
        public const string CookiePath = "/admin/";
        public const string InfoKey = "ainfo";
        public const string PermissionKey = "apermission";
        public const string BtnPermissionKey = "btnpermission";
        public const string AuthInfoKey = "authinfo";
        public const string IsAuth = "IsAuth";
        public const string theme = "theme";
        public const string WaitDeal = "waitDeal";
        private const string page = "page";
        private const string rows = "rows";
        private const string sort = "sort";
        private const string order = "order";
        private const string comboTotal = "comboTotal";
        private const string comboDetail = "comboDetail";
        private const string StorageInForm = "StorageInForm";
        private const string StorageOutForm = "StorageOutForm";
        private const string StoreForm = "StoreForm";
        private const string PcOrderForm = "PcOrderForm";


        /// <summary>
        /// Http上下文
        /// </summary>
        static HttpContext ContextHttp
        {
            get
            {
                return HttpContext.Current;
            }
        }
        static System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return ContextHttp.Session;
            }
        }

        static HttpResponse Response
        {
            get
            {
                return ContextHttp.Response;
            }
        }

        static HttpRequest Request
        {
            get
            {
                return ContextHttp.Request;
            }
        }

        /// <summary>
        /// 用户基本信息
        /// </summary>
        public static UserInfo sessUserInfo
        {
            get
            {
                if (Session[InfoKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[InfoKey] as UserInfo;
            }

        }



        #region 根据Cookie生成Session数据
        private static void GetSessionByCookie()
        {
            if (Request.Cookies[InfoKey] != null)
            {


                string strUserInfo = SecurityHelper.DecryptUserInfo(Request.Cookies[InfoKey].Value);
                int userId = int.Parse(strUserInfo);
                View_BD_UserDepInfo bdUsr = DALSession.Current.View_BD_UserDepInfoDAL.GetListBy(u => u.ID == userId).FirstOrDefault();
                UserInfo usr = new UserInfo() { Id = bdUsr.ID, DepId = bdUsr.DepId, DepName = bdUsr.DepName, AccountLogIn = bdUsr.AccountLogIn, Name = bdUsr.UserName, SchoolCode = bdUsr.SchoolCode, SchoolName = bdUsr.SchoolName, UserCode = bdUsr.UserCode, Age = (int)bdUsr.Age, IfHeadquarters = bdUsr.IfHeadquarters };
                
                Session[PermissionKey] = Permission.GetPermByUserId(userId);
                Session[BtnPermissionKey] = Permission.GetBtnPermByUserId(userId);


                Session[InfoKey] = usr;
            }
            else
            {
                Response.Redirect("/Home/Login");
            }

        }
        #endregion

        public static List<BD_Permission> SessPermission
        {
            get
            {

                if (Session[PermissionKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[PermissionKey] as List<BD_Permission>;
            }

        }

        public static List<BD_btnPermission> SessbtnPermission
        {
            get
            {
                if (Session[BtnPermissionKey] == null)
                {
                    GetSessionByCookie();
                }
                return Session[BtnPermissionKey] as List<BD_btnPermission>;
            }

        }



        public static List<BD_ComboboxTotal> SessComboboxTotal
        {
            get
            {
                if (Session[comboTotal] == null)
                {

                    Session[comboTotal] = DALSession.Current.BD_ComboboxTotalDAL.GetListBy(c => 1 == 1).ToList();
                }
                return Session[comboTotal] as List<BD_ComboboxTotal>;
            }

        }

        public static List<BD_ComboboxDetail> SessComboboxDetail
        {
            get
            {
                if (Session[comboDetail] == null)
                {

                    Session[comboDetail] = DALSession.Current.BD_ComboboxDetailDAL.GetListBy(c => 1 == 1).ToList();
                }
                return Session[comboDetail] as List<BD_ComboboxDetail>;
            }

        }



        //订单查询条件保存
        //public static View_Audit_PC_Order SessPcOrder
        //{
        //    get
        //    {

        //        if (Session[PcOrderForm] == null)
        //        {
        //            return new View_Audit_PC_Order();
        //        }
        //        else
        //        {
        //            return Session[PcOrderForm] as View_Audit_PC_Order;

        //        }

        //    }
        //    set
        //    {
        //        Session[PcOrderForm] = value;
        //    }

        //}




        public static int Page
        {
            get
            {
                if (Request.Form[page] != null)
                {
                    return Commons.String2Int32(Request.Form[page].ToString());
                }
                else
                {
                    return 1;
                }
            }
        }

        public static int Rows
        {
            get
            {
                if (Request.Form[rows] != null)
                {
                    return Commons.String2Int32(Request.Form[rows].ToString());
                }
                else
                {
                    return 1;
                }
            }
        }
        public static string Sort
        {
            get
            {
                if (Request.Form[sort] != null)
                {
                    return Request.Form[sort].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public static bool Order
        {
            get
            {
                if (Request.Form[order] != null)
                {
                    if (string.Equals(Request.Form[order].ToString(), "asc"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
        }

        public static bool OrderDesc
        {
            get
            {
                if (Request.Form[order] != null)
                {
                    if (string.Equals(Request.Form[order].ToString(), "asc"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
        }

    }
}